package com.attendance.www.hr.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.HolidayBean;
import com.attendance.www.hr.beans.HolidayPageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IHolidayImportService;
import com.attendance.www.hr.service.IHolidayService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 节假日查询
 * 
 * @version 2017/10/19
 * @author 徐伟
 */
@Controller
@RequestMapping("/holiday")
public class HolidayController {

	@Resource
	private IHolidayService holidayService;
	@Resource
	private IHolidayImportService holidayImportService;
	
	/**
	 * 模板下载
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getHolidayURL.do", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody 
	public Object getHolidayURL(HttpServletRequest request){
		return AppConstant.TEMPLATE_RESOURCE+"/法定节假日导入模板.xlsx";
	}
	/**
	 * 编码信息导入接口
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/importHoliday.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object importCode(HttpServletRequest request) throws Exception {
		
		String attachmentId = request.getParameter("attachmentId");
		//获取类型：增量，覆盖
		String type = request.getParameter("type");
		
		if (StringUtils.isBlank(attachmentId) || StringUtils.isBlank(type)) {
			throw new ResultFaultException("0_00001");
		}
		
	    UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
	
		//第二步导入编码信息数据
		List<String> successList=holidayImportService.importCode(Integer.parseInt(attachmentId),type,userEmployee.getUserId());

		return ResultMap.success(successList);
	}
	/**
	 * 获取所有节假日
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getHolidayList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getHolidayList(HttpServletRequest request) {
		String holidayYear = request.getParameter("holidayYear");
		if (StringUtils.isBlank(holidayYear)) {
			throw new ResultFaultException("0_00001");
		}
		return ResultMap.success(holidayService.getHolidayList(holidayYear));
	}
	/**
	 * 获取所有节假日，带分页
	 * @param request
	 * @param holidayBean
	 * @return
	 */
	@RequestMapping(value = "/getHolidayListByPageCount.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getHolidayListByPageCount(HttpServletRequest request, @RequestBody HolidayBean holidayBean) {

		Integer pageNo = holidayBean.getPageNo();
		Integer pageCount = holidayBean.getPageCount();

		if (pageNo == null || pageCount == null) {
			throw new ResultFaultException("0_00001");
		}

		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		HolidayPageBean holidayPageBean = new HolidayPageBean();
		holidayPageBean = holidayService.getHolidayListByPageCount(holidayBean, startNum, pageCount);
		return ResultMap.success(holidayPageBean);
	}
	/**
	 * 删除一条节假日信息
	 * @param request
	 * @param holidayBean
	 * @return
	 */
	@RequestMapping(value = "/deleteHoliday.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteHoliday(HttpServletRequest request, @RequestBody HolidayBean holidayBean) {
		if (holidayBean.getHolidayId()==null) {
			throw new ResultFaultException("0_00001");
		}

		holidayService.deleteHoliday(holidayBean.getHolidayId());
		return ResultMap.success();
	}

}
